package hot100;

public class addTwoNumbers2 {
    public mergeKLists.ListNode addTwoNumbers(mergeKLists.ListNode l1, mergeKLists.ListNode l2) {
        mergeKLists.ListNode cur = new mergeKLists.ListNode(0);
        mergeKLists.ListNode ret = cur;
        int sum = 0;
        while(l1 != null || l2 != null) {
            if(l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if(l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            mergeKLists.ListNode tem = new mergeKLists.ListNode(sum % 10);
            sum = sum / 10 ;
            cur.next = tem;
            cur = cur.next;
        }
        while(sum != 0) {
            mergeKLists.ListNode tem = new mergeKLists.ListNode(sum % 10);
            sum = sum / 10 ;
            cur.next = tem;
            cur = cur.next;
        }
        return ret.next;

    }
}
